<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="16">
            <a-col :sm="24" :md="12" :lg="8" :xl="6">
              <a-form-item label="表名">
                <a-input v-model="queryParam.tableName" placeholder="请输入表名" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :sm="24" :md="12" :lg="8" :xl="6">
              <a-form-item label="表注释">
                <a-input v-model="queryParam.comment" placeholder="请输入表注释" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :sm="24" :md="12" :lg="8" :xl="6">
              <span class="table-page-search-submitButtons">
                <a-space>
                  <a-button type="primary" @click="handleQuery"><a-icon type="search"/>查询</a-button>
                  <a-button @click="handleReset"><a-icon type="redo"/>重置</a-button>
                  <a @click="advanced = !advanced">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'"/>
                  </a>
                </a-space>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 按钮 -->
      <div class="table-operations">
        <a-button type="primary" icon="plus" @click="$refs.saveForm.handleAdd()">新增</a-button>
        <a-button type="primary" icon="download" :disabled="multiple" @click="$refs.generateCode.handleGenerateBatch(selectedRowKeys)">批量生成</a-button>
        <a-button type="danger" icon="delete" :disabled="multiple" @click="handleRemoveBatch">批量删除</a-button>
        <table-setting
          :style="{float: 'right'}"
          :table-size.sync="tableSize"
          v-model="columns"
          :refresh-loading="loading"
          @refresh="getList"/>
      </div>
      <!-- 表格 -->
      <a-table
        :scroll="{x: true}"
        :loading="loading"
        :size="tableSize"
        rowKey="id"
        :columns="columns"
        :data-source="genTableList"
        :row-selection="{selectedRowKeys: selectedRowKeys, onChange: handleSelectRow}"
        :pagination="false"
        :bordered="tableBordered">
        <template v-slot:pageType="value, row">
          {{ ['', '单表'][row.pageType] }}
        </template>
        <template v-slot:formType="value, row">
          {{ ['', '抽屉', '模态框'][row.formType] }}
        </template>
        <template v-slot:isPc="value, row">
          <a-tag v-if="row.isPc == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:isSave="value, row">
          <a-tag v-if="row.isSave == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:isUpdate="value, row">
          <a-tag v-if="row.isUpdate == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:isRemove="value, row">
          <a-tag v-if="row.isRemove == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:isRemoveBatch="value, row">
          <a-tag v-if="row.isRemoveBatch == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:isImport="value, row">
          <a-tag v-if="row.isImport == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:isExport="value, row">
          <a-tag v-if="row.isExport == 1" color="green">开启</a-tag>
          <a-tag v-else color="red">关闭</a-tag>
        </template>
        <template v-slot:operation="value, row">
          <a @click="$refs.updateForm.handleUpdate(row)"><a-icon type="edit"/>修改</a>
          <a-divider type="vertical"/>
          <a @click="$refs.previewCode.handlePreview(row)"><a-icon type="eye"/>预览</a>
          <a-divider type="vertical"/>
          <a @click="$refs.generateCode.handleGenerate(row.id)"><a-icon type="download"/>生成</a>
          <a-divider type="vertical"/>
          <a @click="handleRemove(row)"><a-icon type="delete"/>删除</a>
        </template>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="handleSizeChange"
        @change="handleSizeChange"/>
      <!-- 新增 -->
      <save-form ref="saveForm" @ok="getList"/>
      <!-- 修改 -->
      <update-form ref="updateForm" @ok="getList"/>
      <!-- 预览 -->
      <preview-code ref="previewCode"/>
      <!-- 预览 -->
      <generate-code ref="generateCode"/>
    </a-card>
  </page-header-wrapper>
</template>
<script>
import {tableMixin} from '@/store/table-mixin'
import SaveForm from './modules/SaveForm'
import UpdateForm from './modules/UpdateForm'
import PreviewCode from './modules/PreviewCode'
import GenerateCode from './modules/GenerateCode'
import {listGenTable, removeBatchGenTable, removeGenTable} from '@/api/generate/genTable'

export default {
  components: {
    SaveForm,
    UpdateForm,
    PreviewCode,
    GenerateCode
  },
  mixins: [tableMixin],
  data() {
    return {
      genTableList: [],
      selectedRowKeys: [],
      selectedRows: [],
      advanced: false,
      single: true,
      multiple: true,
      loading: false,
      total: 0,
      queryParam: {
        tableName: undefined,
        comment: undefined,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        {dataIndex: 'author', title: '作者', align: 'center'},
        {dataIndex: 'parentPackage', title: '父级包名', align: 'center'},
        {dataIndex: 'mavenModule', title: 'maven模块名', align: 'center'},
        {dataIndex: 'module', title: '模块名', align: 'center'},
        {dataIndex: 'comment', title: '表注释', align: 'center'},
        {dataIndex: 'tableName', title: '表名', align: 'left'},
        {dataIndex: 'business', title: '业务名', align: 'left'},
        {dataIndex: 'pkJavaField', title: '主键属性名', align: 'center'},
        {dataIndex: 'pkColumnName', title: '主键列名', align: 'center'},
        {dataIndex: 'pageType', title: '页面类型', align: 'center', scopedSlots: {customRender: 'pageType'}},
        {dataIndex: 'formType', title: '表单类型', align: 'center', scopedSlots: {customRender: 'formType'}},
        {dataIndex: 'isPc', title: 'PC端', align: 'center', scopedSlots: {customRender: 'isPc'}},
        {dataIndex: 'isSave', title: '新增', align: 'center', scopedSlots: {customRender: 'isSave'}},
        {dataIndex: 'isUpdate', title: '更新', align: 'center', scopedSlots: {customRender: 'isUpdate'}},
        {dataIndex: 'isRemove', title: '删除', align: 'center', scopedSlots: {customRender: 'isRemove'}},
        {dataIndex: 'isRemoveBatch', title: '批量删除', align: 'center', scopedSlots: {customRender: 'isRemoveBatch'}},
        {dataIndex: 'isImport', title: '导入', align: 'center', scopedSlots: {customRender: 'isImport'}},
        {dataIndex: 'isExport', title: '导出', align: 'center', scopedSlots: {customRender: 'isExport'}},
        {dataIndex: 'operation', title: '操作', width: '240', align: 'center', fixed: 'right', scopedSlots: {customRender: 'operation'}}
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listGenTable(this.queryParam).then(res => {
        this.genTableList = res.data
        this.total = res.total
        this.loading = false
      })
    },
    /** 点击搜索 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 点击重置 */
    handleReset() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined
      }
      this.handleQuery()
    },
    /** 分页变化 */
    handleSizeChange(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 选择行 */
    handleSelectRow(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 点击删除 */
    handleRemove(row) {
      this.$confirm({
        title: '删除提示',
        content: '确认删除所选中数据?',
        onOk: () => {
          removeGenTable(row.id).then(res => {
            this.$message.success(res.msg)
            this.getList()
          })
        }
      })
    },
    /** 点击批量删除 */
    handleRemoveBatch() {
      this.$confirm({
        title: '批量删除提示',
        content: '确认删除所选中数据?',
        onOk: () => {
          removeBatchGenTable(this.this.selectedRowKeys).then(res => {
            this.$message.success(res.msg)
            this.getList()
          })
        }
      })
    }
  }
}
</script>
